<script type="text/javascript" >
    stFacebookToolbox.load_tooltip();
    stFacebookToolboxcustom.on_submitform_new('#uploadObject');
//    stFacebookToolbox.on_submitform('#uploadObject', function(data){
//       // stFacebookToolbox.loadPage('gallery_app', '', '<?php _e("Successful", $config['plugin_name']) ?>');
//    });
    stFacebookToolbox.loadUploadButton('200');
    stFacebookToolbox.loadRemoveButton();
    stFacebookToolbox.shortAction();
        jQuery("a[rel^='prettyPhoto']").prettyPhoto({
            theme:'facebook',
            default_height:100,
            keyboard_shortcuts: false
        });
    function add_image(){
        jQuery('.upload_field').slideDown();
        jQuery("#id").val('');
        jQuery("#name").val('');
        jQuery.each(jQuery("input[type='checkbox']"),function(index,value){
        value.checked = false;
        });
        jQuery("#description").val('');
        jQuery("#album_id").val(jQuery("#album").val());
        jQuery("#id_upload_file").val('');
        jQuery("#reset_upload_file").addClass('hide');
        jQuery("#image_upload_file").attr('style','display:none');
    }
    jQuery("#tabs ul li").each(function(){
        if(jQuery(this).children().attr('action') == 'gallery_app' || jQuery(this).children().attr('action') == 'coupon')
            jQuery(this).hide();
        if(jQuery(this).children().attr('action') == 'home_applications')
            jQuery(this).attr('class','current');
        });
</script>
<style>

</style>
<?php 
if(isset($edit)){
    $display = 'display: block;';
    $edit_data = json_decode($edit);
    $check = true;
}
else 
{
    $display = 'display: none;';
    $check = false;
}
?>
<div class="w100p bgl">
    <div class="w100p bgr p5b">
        <ul class="postcardAction" style="margin-left: 10px;padding-top: 15px;">
                <li>
                    <a class="button add-new-h2 anchor" onclick="add_image();">
                        Upload new picture
                    </a>
                </li>
            </ul>
        <div class="upload_field" style="<?php echo $display;?>padding-left:13px;">
                <form method="post" action="<?php echo admin_url('/admin.php?page=' . $config['plugin_name'] . '&action=' . $current_action) ?>" style="display: block;" id="uploadObject" class="stform">
                    <ul>
                        <li>
                            <label>Name:</label>
                            <div class="required">
                                <input type="text" name="data[name]" id="name" class="required" <?php if($check){echo "value='{$edit_data->name}'";} ?> />
                            </div>
                                <input type="hidden" name="data[id]" id="id" <?php if($check){echo "value='{$edit_data->id}'";} ?> />
                                <input type="hidden" name="album_id" id="album_id" <?php if($check){echo "value='{$album_id}'";} ?> />
                                <input type="hidden" name="cur_page" id="cur_page" <?php echo "value='{$cur_page}'"; ?> />
                            <br clear="all">
                        </li>
                        <li>
                            <label>Description:</label>
                            <div class="">
                                <textarea name="data[description]" id="description" class="" ><?php if($check){echo $edit_data->description;} ?></textarea>
                            </div>
                            <br clear="all">
                        </li>
                        <li>
                            <label><?php _e('Album', $config['plugin_name']); ?>:</label>
                            <div class="required">
                                <?php foreach($album as $al) {
                                    $checked = '';
                                    if($model->check_album($edit_data->id,$al->id)){
                                        $checked = 'checked';
                                    }
                                     echo  '<input type="checkbox" name="data[album][]" value="'.$al->id.'" '.$checked.'> ' . $al->name.'<br />';
                                }
        ?>
                            </div>
                            <br clear="both" />
                        </li>
                        <li>
                            <label>Upload file:</label>
                            <div class="required">
                                <?php
                        $class_hide = "hide";
                        if (!empty($edit_data->file_name)) {
                            $upload_data = wp_upload_dir();
                            $picture = explode('wp-content/',$edit_data->file_name);
                            $picture = '../wp-content/'.$picture[1];
                            $class_hide = "";
                        }
                        ?>
                                <input type="hidden" <?php if($check){echo "value='{$edit_data->file_name}'";} ?> id="id_upload_file" name="data[upload_file]" class="required" value="">
                                <div class="upload_button_div">
                                    <span class="button image_upload_button" id="upload_file">Upload Image</span>
                                    <span class="button image_reset_button <?php echo $class_hide ?>" id="reset_upload_file" pictureid="upload_file">Remove</span>
                                </div>  
                                <?php
                             if ($edit_data->amazon_name != '') {
                                 ?>
                            <img alt="" src="https://s3-ap-southeast-1.amazonaws.com/<?php echo $config['gallery_bucket'];?>/<?php echo 'fb-app-gallery/'.substr($edit_data->amazon_name,0,strlen($edit_data->amazon_name)-4).'-thumb'.substr($edit_data->amazon_name,-4,4); ?>?cache=<?php echo rand(1,1000); ?>" id="image_upload_file" class="hide preview" style="display: inline;"/>
                        <?php } ?>
                            </div>
                            <br clear="all">
                        </li>
                        <li>
                            <input type="submit" value="Submit">
                            <input type="button" onclick="jQuery('.upload_field').slideUp()" value="Close">
                        </li>
                    </ul>
                    <br style="clear:both;" />
                </form>
            </div>
<select name="" style="margin:10px;" onchange="stFacebookToolbox.loadPage('gallery_app&album_id='+this.value);" id="album">
    <option value="all">All album&nbsp;</option>
    <?php foreach($album as $filter):?>
        <option value="<?php echo $filter->id;?>" <?php if($album_id == $filter->id){echo 'selected';} ?>><?php echo $filter->name;?>&nbsp;</option>
    <?php endforeach;?>
</select>
<table class="widefat" cellspacing="0">
                <thead>
                    <tr>
                        <?php print_column_headers('stFacebookToolbox_stFacebookToolbox'); ?>
                    </tr>
                </thead>

                <tbody>
                <?php
                if (count($results) > 0) {
                    
                    $tr_class = "";
                    $upload_data = wp_upload_dir();
                    foreach ($results as $result) {
                        $image = $result->amazon_name;
                        $image = substr($image,0,-4).'-thumb'.substr($image,strlen($image)-4,4).'?cache='.  rand(0, 1000);
                        if( empty($tr_class) || $tr_class == "class='bg_gray2'"){
                            $tr_class = "class='bg_gray1'";
                        }else{
                            $tr_class = "class='bg_gray2'";
                        }
                        echo "<tr id='$result->id' $tr_class >"
                        . "<td>" . $result->id

                        . "</td>"
                        . "<td>" . $result->name
                        . "<div class=\"row-actions short_action\">"
                        . "<div onclick='stFacebookToolboxcustom.loadPage(\"gallery_app&id=$result->id&edit=true&album_id=\"+jQuery(\"#album\").val()+\"&cur_page=\"+jQuery(\"#cur_page\").val());' style='cursor:pointer;'>Edit | </div>"
                        . "<span class='trash' short_action='delete' id='$result->id' confirm='Are you sure you want to delete this picture?' style='cursor:pointer;'>Trash </span>"
                        . "</div>"
                        . "</td>"
                        . "<td><a rel='prettyPhoto' href='https://s3-ap-southeast-1.amazonaws.com/{$config['gallery_bucket']}/fb-app-gallery/{$result->amazon_name}'><img src='https://s3-ap-southeast-1.amazonaws.com/{$config['gallery_bucket']}/fb-app-gallery/" . $image . "' width='70' alt='$result->description' title='$result->description' /></a>"
                        . "</td>"
                        . "<td>" . $result->description
                        . "<td>" . substr(str_replace('-','.',$result->last_update),0,-3)
                        . "</td>"
                        . "</td>"
                        
                        . "</tr>"
                        ;
                    }
                }
                ?>
                </tbody>
            </table>
            <div class="page_d">
                <?php echo $pagination?>
                <div class="cl"></div>
            </div>
    </div>
</div>